<template>
  <el-dialog v-if="popenevTVisible" title="发文号" :visible.sync="popenevTVisible" :before-close="handleCancel" width="450px" :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="年度" prop="nd">
            <el-select v-model="form.nd" filterable placeholder="请选择年度">
              <el-option v-for="(item, index) in yearList" :key="index" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="流水名" prop="lsm">
            <el-select v-model="form.lsm" filterable placeholder="请选择流水名" @change="handleLsmChange">
              <el-option v-for="(item, index) in lsmList" :key="index" :label="item.lsm" :value="item.lsm">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="发文字" prop="fwz">
            <el-select v-model="form.fwz" filterable placeholder="请选择发文字" @change="handleFwzChangeGetFwh">
              <el-option v-for="(item, index) in fwzList" :key="index" :label="item" :value="item">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="!form.notShowLsh">
          <el-form-item label="流水号" prop="lsh">
            <el-input v-model="form.lsh" @input="handleKeyup" style="width:241px" placeholder="请输入流水号">
              <el-tooltip popper-class="hc-dec-text" slot="append" effect="light" content="查询漏号" placement="bottom-end">
                <el-button icon="el-icon-search" @click="getlh"></el-button>
              </el-tooltip>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="showLh">
          <el-form-item label="漏号" prop="lsh">
            <el-select v-if="lhList.length > 0" v-model="form.lh" @change="form.lsh = form.lh" filterable placeholder="请选择流水名">
              <el-option v-for="(item, index) in lhList" :key="index" :label="item" :value="item">
              </el-option>
            </el-select>
            <div v-else>无漏号</div>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="!form.notShowLsh">
          <el-form-item label="发文文号" prop="fwz">
            {{gfwh}}
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleOk">确定</el-button>
      <el-button @click="handleCancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getFwhList } from "../../api/module/fwhgl/fwhgl";
import { getflsh, getlh } from "../../api/module/fwgl/fwgl";
let nowYear = new Date().getFullYear();
const yearList = [{ label: nowYear - 1, value: nowYear - 1 }, { label: nowYear, value: nowYear }, { label: nowYear + 1, value: nowYear + 1 }]
export default {
  data()
  {
    return {
      showLh: false,
      popenevTVisible: true,
      multiple: false,
      //promise 状态
      promiseStatus: null,
      form: {},
      fwzList: [],//发文字列表
      yearList: yearList,//年度列表
      lsmList: [],//流水号列表
      lhList: [],//漏号列表
      rules: {
        pwd: [
          { required: true, message: "请输入密码", trigger: "blur" },

        ],
        passwordCheck: [

        ],
      }
    };
  },
  computed: {
    gfwh()
    {
      if (this.form.fwzgs)
      {
        return this.form.fwzgs.replace('Z', this.form.fwz).replace('Y', this.form.nd).replace("#", this.form.lsh)
      } else
      {
        return ""
      }
    }
  },
  methods: {
    //显示发文给号弹窗
    show(P_FormData)
    {
      this.reset();
      if (P_FormData && P_FormData.fwz)
      {
        this.form.nd = P_FormData.nd;
        this.form.lsm = P_FormData.lsm;
        this.form.fwz = P_FormData.fwz;
      }
      if(P_FormData && P_FormData.notShowLsh)
      {
        this.form.notShowLsh = P_FormData.notShowLsh;
      }
      this.getFwhList();
      // 创建promise
      return new Promise((resolve, reject) =>
      {
        this.promiseStatus = {
          resolve,
          reject,
        };
      });
    },
    //限制流水号只能输入数字
    handleKeyup(value)
    {
      // 只允许输入数字
      this.form.lsh = value.replace(/[^\d]/g, '');
    },
    //确定
    handleOk()
    {
      this.promiseStatus.resolve({ gfwh: this.gfwh, form: this.form });
      this.destroyElement();
    },
    //取消
    handleCancel()
    {
      //清空勾选项
      this.promiseStatus.reject({
        msg: "取消",
      });

      this.destroyElement();
    },
    /**查询漏号 */
    getlh()
    {
      getlh({
        fwwhNd: this.form.nd,
        fwwhLsm: this.form.lsm,
        fwwhFwz: this.form.fwz
      }).then(res =>
      {
        this.lhList = res.result && res.result.data || [];
        this.showLh = true;
      })
    },
    // 表单重置
    reset()
    {
      this.form = {
        unid: null,
        nd: new Date().getFullYear(),
        lsm: null,
        fwz: "",
        lh: "",
        lsh: "",
        gfwh: null,
        fwzgs: ""
      };
      console.log(this.form);
      this.resetForm("form");
    },
    //获取发文号列表
    getFwhList()
    {
      getFwhList({
        pageSize: 1000,
        pageNo: 1,
      }).then(res =>
      {
        this.lsmList = res.result.data || [];
        if (this.form.lsm)
        {
          this.handleLsmChange(this.form.lsm);
        }
        else if (res.result.data.length === 1)
        {
          this.fwhObj.lsm = res.result.data[0].lsm
          this.handleLsmChange(this.fwhObj.lsm)
        }
      })
    },
    //监听流水名修改
    handleLsmChange(e)
    {
      this.lsmList.map(item =>
      {
        if (item.lsm === e)
        {
          this.fwzList = item.fwz.split(";");
          this.form.fwzkh = item.fwzkh;
          this.form.fwzgs = item.fwzgs;
        }
      });
      this.form.lsh = "";
      if(!this.form.fwz)
      {
        this.form.fwz = "";
      }
      if(this.form.fwz)
      {
        this.clickGetFwh();
      }
    },
    // 发文字Change事件
    handleFwzChangeGetFwh()
    {
      this.clickGetFwh()
    },
    //获取流水号
    clickGetFwh()
    {
      getflsh({
        fwwhNd: this.form.nd,
        fwwhLsm: this.form.lsm,
        fwwhFwz: this.form.fwz
      })
        .then(res =>
        {
          this.form.lsh = res.message;
        })
        .catch(err =>
        {
          console.log(err);
        });
    },
    /**
     * cs 2022-4-8
     * 销毁组件
     */
    destroyElement()
    {
      this.popenevTVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>